package com.jader.myviews;

import com.jader.activities.Seller;
import com.jader.beans.Goods;
import com.jader.util.JaderApplication;
import com.zlp.jader.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;

@SuppressLint("CommitPrefEdits")
public class BabyPopWindow implements OnDismissListener, OnClickListener {

	private TextView pop_ok, pop_num, pop_add, pop_reduce, pop_price;
	private ImageView pop_del;
	private PopupWindow popupWindow;
	private OnItemClickListener listener;
	private Context context;
	private Goods goods;
	private int number = 1;

	public BabyPopWindow(Context context) {
		this.context = context;
		View view = LayoutInflater.from(this.context).inflate(
				R.layout.adapter_popwindow, null);
		pop_add = (TextView) view.findViewById(R.id.pop_add);
		pop_reduce = (TextView) view.findViewById(R.id.pop_reduce);
		pop_num = (TextView) view.findViewById(R.id.pop_num);
		pop_ok = (TextView) view.findViewById(R.id.pop_ok);
		pop_price = (TextView) view.findViewById(R.id.pop_price);
		pop_del = (ImageView) view.findViewById(R.id.iv_adapter_grid_pic);

		pop_add.setOnClickListener(this);
		pop_reduce.setOnClickListener(this);
		pop_ok.setOnClickListener(this);
		pop_del.setOnClickListener(this);

		popupWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT);
		popupWindow.setAnimationStyle(R.style.AnimationFade);
		popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		popupWindow.setOnDismissListener(this);
		popupWindow.setOutsideTouchable(true);
	}

	public interface OnItemClickListener {
		public void onClickOKPop();
	}

	public boolean isShowing() {
		return popupWindow.isShowing();
	}

	public void setOnItemClickListener(OnItemClickListener listener) {
		this.listener = listener;
	}

	@Override
	public void onDismiss() {
		Seller seller = (Seller) context;
		seller.setClickBuy(0);
		listener.onClickOKPop();

	}

	public void showAsDropDown(View parent) {
		popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
		popupWindow.setFocusable(true);
		popupWindow.setOutsideTouchable(true);
		popupWindow.update();
	}

	public void dissmiss() {
		popupWindow.dismiss();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.pop_add:
			if (!pop_num.getText().toString()
					.equals(String.valueOf(goods.getStock()))) {
				number++;
				pop_num.setText(number + "");
			}
			break;
		case R.id.pop_reduce:
			if (!pop_num.getText().toString().equals("1")) {
				number--;
				pop_num.setText(number + "");
			}
			break;
		case R.id.pop_ok:
			listener.onClickOKPop();
			dissmiss();

			break;
		default:
			break;
		}
	}

	public Goods getGoods() {
		return goods;
	}

	public void setGoods(Goods goods) {
		this.goods = goods;
		initData();
	}

	private void initData() {
		JaderApplication.getBitmapUtils().display(pop_del,
				JaderApplication.getIP() +"/Jader"+ goods.getPicture());
		pop_price.setText("￥" + goods.getPrice() + "   " + "库存"
				+ goods.getStock());
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

}
